<?php


namespace api\modules\manage\services;

use api\modules\manage\models\DictElements;
use api\modules\manage\resources\ElementResource;
use Yii;

class ElementService extends CommonService
{
    public $resourceClass = 'api\modules\manage\resources\ElementResource';

    protected function getQuery()
    {
        if(is_array($this->params['type'])){
            if(count($this->params['type']) > 0){
                foreach ($this->params['type'] as $v){
                    if(!in_array($v,ElementResource::ALLOW_TYPES)){
                        throw new \Exception(Yii::t('app', 'params type error'));
                    }
                }
            }else{
                throw new \Exception(Yii::t('app', 'params type error'));
            }
        }else {
            if (!in_array($this->params['type'], ElementResource::ALLOW_TYPES)) {
                throw new \Exception(Yii::t('app', 'params type error'));
            }
        }
        $conditions = ElementResource::getCondition($this->params);
        $query = DictElements::find();
        $query = ElementResource::setQueryCondition($query,$this->params,$conditions);
        return $query;
    }
}